package cn.com.inossem.model;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 报销实体类
 * Created by Mickey.Li on 2017-8-25.
 */

public class ExpenseApplyBean implements Parcelable {
	public Project project;
	public String no;
	public String approveMail;
	public double sum = 0;
	public List<ExpenseItemBean> items = new ArrayList<>();
	public Date createDate;
	public ProjectBean projectInfoUser;
	public String id;
	public int status = 0;

	public String items2String() {
		String data = "";
		for (ExpenseItemBean bean : items) {
			data += items.toString();
		}
		return data;
	}

	public void setProjectId(String id) {
		project = new Project(id);
	}

	public static class Project implements Parcelable {
		public String id;
		public String projectName;

		public Project(String id) {
			this.id = id;
		}

		@Override
		public int describeContents() {
			return 0;
		}

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			dest.writeString(this.id);
			dest.writeString(this.projectName);
		}

		protected Project(Parcel in) {
			this.id = in.readString();
			this.projectName = in.readString();
		}

		public static final Creator<Project> CREATOR = new Creator<Project>() {
			@Override
			public Project createFromParcel(Parcel source) {
				return new Project(source);
			}

			@Override
			public Project[] newArray(int size) {
				return new Project[size];
			}
		};
	}

	public static class ExpenseItemBean implements Parcelable {
		public String id = "";
		public String explain;
		public String wbs;
		public String invoiceTyp;
		public String summary;
		public double amount = 0;

		@Override
		public String toString() {
			return "ExpenseItemBean{" +
					"id='" + id + '\'' +
					", explain='" + explain + '\'' +
					", wbs='" + wbs + '\'' +
					", invoiceTyp='" + invoiceTyp + '\'' +
					", summary='" + summary + '\'' +
					", amount=" + amount +
					'}';
		}

		@Override
		public int describeContents() {
			return 0;
		}

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			dest.writeString(this.id);
			dest.writeString(this.explain);
			dest.writeString(this.wbs);
			dest.writeString(this.invoiceTyp);
			dest.writeString(this.summary);
			dest.writeDouble(this.amount);
		}

		public ExpenseItemBean() {
		}

		ExpenseItemBean(Parcel in) {
			this.id = in.readString();
			this.explain = in.readString();
			this.wbs = in.readString();
			this.invoiceTyp = in.readString();
			this.summary = in.readString();
			this.amount = in.readDouble();
		}

		public static final Parcelable.Creator<ExpenseItemBean> CREATOR = new Parcelable.Creator<ExpenseItemBean>() {
			@Override
			public ExpenseItemBean createFromParcel(Parcel source) {
				return new ExpenseItemBean(source);
			}

			@Override
			public ExpenseItemBean[] newArray(int size) {
				return new ExpenseItemBean[size];
			}
		};

		public boolean checkData() {
			return !TextUtils.isEmpty(summary) && !TextUtils.isEmpty(wbs) && !TextUtils.isEmpty(invoiceTyp) && amount != 0;
		}
	}

	public static String expense2Json(ExpenseApplyBean bean) {
		return new GsonBuilder().setDateFormat("yyyy.MM.dd").create().toJson(bean, ExpenseApplyBean.class);
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeParcelable(this.project, flags);
		dest.writeString(this.no);
		dest.writeString(this.approveMail);
		dest.writeDouble(this.sum);
		dest.writeTypedList(this.items);
		dest.writeLong(this.createDate != null ? this.createDate.getTime() : -1);
		dest.writeParcelable(this.projectInfoUser, flags);
		dest.writeString(this.id);
		dest.writeInt(this.status);
	}

	public ExpenseApplyBean() {
	}

	protected ExpenseApplyBean(Parcel in) {
		this.project = in.readParcelable(Project.class.getClassLoader());
		this.no = in.readString();
		this.approveMail = in.readString();
		this.sum = in.readDouble();
		this.items = in.createTypedArrayList(ExpenseItemBean.CREATOR);
		long tmpCreateDate = in.readLong();
		this.createDate = tmpCreateDate == -1 ? null : new Date(tmpCreateDate);
		this.projectInfoUser = in.readParcelable(ProjectBean.class.getClassLoader());
		this.id = in.readString();
		this.status = in.readInt();
	}

	public static final Creator<ExpenseApplyBean> CREATOR = new Creator<ExpenseApplyBean>() {
		@Override
		public ExpenseApplyBean createFromParcel(Parcel source) {
			return new ExpenseApplyBean(source);
		}

		@Override
		public ExpenseApplyBean[] newArray(int size) {
			return new ExpenseApplyBean[size];
		}
	};
}
